﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace LCL.XPing
{
    public partial class PingPanel : Form
    {
        private string url;
        private Hashtable webFlg = new Hashtable();

        private List<string> website = new List<string>();
        private readonly BindingList<PingData> collection = new BindingList<PingData>();
        private StateValue stateValue = new StateValue();
        private Hashtable tmTable = new Hashtable();
        public PingPanel()
        {
            InitializeComponent();
        }
        private void but_Ping_Click(object sender, EventArgs e)
        {
            this.tePing.Text = this.tePing.Text.Trim().ToLower();
            if (this.tePing.Text == "")
            {
                this.tePing.Focus();
                return;
            }
            if (this.website.Contains(this.tePing.Text))
            {
                this.tePing.Text = "";
                this.tePing.Focus();
                return;
            }
            this.website.Add(this.tePing.Text);
            this.SetPing(this.tePing.Text);
            this.stateValue = new StateValue();
            this.stateValue.pingtext = this.tePing.Text;
            this.stateValue.ping = new Ping();

            System.Threading.Timer value = new System.Threading.Timer(new TimerCallback(this.OnTimer), this.stateValue, 0, 1000);
            this.tmTable.Add(this.tePing.Text, value);
            this.tePing.Text = "";
            this.tePing.Focus();

        }
        public void SetPing(string website)
        {
            this.collection.Add(new PingData(website));
        }
        private void OnTimer(object obj)
        {
            StateValue stateValue = obj as StateValue;
            string pingtext = stateValue.pingtext;
            try
            {
                PingReply pingReply = stateValue.ping.Send(pingtext, 1000);
                if (pingReply.Status == IPStatus.Success)
                {
                    for (int i = 0; i < this.collection.Count; i++)
                    {
                        if (this.collection[i].website.Equals(pingtext))
                        {
                            this.collection[i].Update(pingReply);
                            this.collection.ResetItem(i);
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < this.collection.Count; j++)
                    {
                        if (this.collection[j].website.Equals(pingtext))
                        {
                            this.collection[j].UpdateErr();
                            this.collection.ResetItem(j);
                        }
                    }
                }
            }
            catch
            {
                for (int k = 0; k < this.collection.Count; k++)
                {
                    if (this.collection[k].website.Equals(pingtext))
                    {
                        this.collection[k].UpdateErr();
                        this.collection.ResetItem(k);
                    }
                }
            }
        }
        private void PingPanel_Load(object sender, EventArgs e)
        {
            this.gcPing.DataSource = this.collection;
            this.gcPing.AutoGenerateColumns = false;
            this.contextMenuStrip1.Hide();
        }
        private void PingPanel_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (this.collection != null && this.collection.Count > 0)
                {
                    this.collection.Clear();
                }
                if (this.stateValue.ping != null)
                {
                    this.stateValue.ping.Dispose();
                }
                foreach (DictionaryEntry item in this.tmTable)
                {
                    (item.Value as System.Threading.Timer).Dispose();
                }
            }
            catch (Exception ex)
            {


            }

        }
        private void gcPing_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.but_Ping_Click(null, null);
            }
        }
        //右键点击行时选中行，并弹出操作菜单
        private void gcPing_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    if (this.gcPing.Rows[e.RowIndex].Selected == false)
                    {
                        this.gcPing.ClearSelection();
                        this.gcPing.Rows[e.RowIndex].Selected = true;
                    }
                    if (this.gcPing.SelectedRows.Count == 1)
                    {
                        this.gcPing.CurrentCell = this.gcPing.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    this.contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);

                    //设置菜单
                    this.url = this.gcPing.SelectedRows[0].Cells[0].Value.ToString();
                    if (this.webFlg.Contains(this.url))
                    {
                        if (Convert.ToInt16(this.webFlg[this.url]) == 0)
                        {
                            this.继续ToolStripMenuItem.Enabled = false;
                            this.停止ToolStripMenuItem.Enabled = true;
                            return;
                        }
                        if (Convert.ToInt16(this.webFlg[this.url]) == 1)
                        {
                            this.继续ToolStripMenuItem.Enabled = true;
                            this.停止ToolStripMenuItem.Enabled = false;
                            return;
                        }
                    }
                    else
                    {
                        this.继续ToolStripMenuItem.Enabled = false;
                        this.停止ToolStripMenuItem.Enabled = true;
                    }
                }
            }
        }
        private void 继续ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Threading.Timer timer = this.tmTable[this.url] as System.Threading.Timer;
            timer.Change(0, 1000);
            this.webFlg[this.url] = 0;

        }
        private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Threading.Timer timer = this.tmTable[this.url] as System.Threading.Timer;
            timer.Change(-1, 1000);
            this.webFlg[this.url] = 1;
        }

        private void 移除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Threading.Timer timer = this.tmTable[this.url] as System.Threading.Timer;
            timer.Dispose();
            this.collection.RemoveAt(this.gcPing.CurrentRow.Index);
            this.tmTable.Remove(this.url);
            this.website.Remove(this.url);
        }
    }
}
